﻿using Core.DataAccess.Model.iTool.Gateway;
using Core.Framework;
using Environment.Interfaces;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Dashboard
{
    public abstract class UpgradeService
    {
        public INodeService iNodeService { get; set; }
        public UpgradeService()
        {
            this.iNodeService = ProviderOfServices.GetService<INodeService>();
        }

        public abstract Task UpgradeAsync();
    }

    public class GatewayUpgradeService : UpgradeService
    {
        public async override Task UpgradeAsync()
        {

            string command = "wget -O install.sh https://cloud.itool.store/iToolGateway_Upgrade.sh && sudo bash install.sh";
            await this.iNodeService.ExcuterCommand("127.0.0.1", command);
        }
    }

    public class GuardUpgradeService : UpgradeService
    {
        public override async Task UpgradeAsync()
        {
            // all node
            var nodes = await this.iNodeService.GetNodes() ?? new List<Node>();
            var command = $"wget -O install.sh https://cloud.itool.store/iToolCetecotor_Upgrade.sh && sudo bash install.sh";
            foreach (var item in nodes)
            {
                await this.iNodeService.ExcuterCommand(item.IP, command);
            }
        }
    }

    public class FileServiceUpgradeService : UpgradeService
    {
        public override async Task UpgradeAsync()
        {
            var instances = await this.iNodeService.GetAllInstances() ?? new List<ServiceInstance>();
            var fileapis = instances.Where(item => item.Title == "iTool-FileApi").ToList() ?? new List<ServiceInstance>();
            var command = $"wget -O installiToolFileCenter.sh https://cloud.itool.store/iToolFileCenter.sh && sudo bash installiToolFileCenter.sh";
            foreach (var item in fileapis)
            {
                await this.iNodeService.ExcuterCommand(item.NodeIP, command);
            }
        }
    }

    public class CloudConnectorUpgradeService : UpgradeService
    {
        public override async Task UpgradeAsync()
        {
            var instances = await this.iNodeService.GetAllInstances() ?? new List<ServiceInstance>();
            var fileapis = instances.Where(item => item.Title == "iTool-Connector").ToList() ?? new List<ServiceInstance>();
            var command = $"wget -O installiToolConnector.sh https://cloud.itool.store/iToolConnector.sh && sudo bash installiToolConnector.sh";
            foreach (var item in fileapis)
            {
                await this.iNodeService.ExcuterCommand(item.NodeIP, command);
            }
        }
    }

    public class CloudServiceUpgradeService : UpgradeService
    {
        public override async Task UpgradeAsync()
        {
            var instances = await this.iNodeService.GetAllInstances() ?? new List<ServiceInstance>();
            var fileapis = instances.Where(item => item.Title == "iTool-Server").ToList() ?? new List<ServiceInstance>();
            var command = $"wget -O installiToolServer.sh https://cloud.itool.store/iToolServer.sh && sudo bash installiToolServer.sh";
            foreach (var item in fileapis)
            {
                await this.iNodeService.ExcuterCommand(item.NodeIP, command);
            }
        }
    }
}
